package com.aplink.generic.widgets;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.Switch;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.dictionary.GenericDictionary;
import com.aplink.generic.interfaces.OnGetObjProInfo;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.pojo.controlspec.SwitchSpec;
import com.aplink.generic.sharepreference.GenericSharePreference;
import com.aplink.generic.util.Language;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.sg.R;

public class ApLinkSwitch extends ApLinkBaseView {
	private View group;
	private final OnCheckedChangeListener mChangeListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(final CompoundButton buttonView,
		        final boolean isChecked) {
			if (ApLinkSwitch.this.mSwitchSpec.isToggleOfflineMode()) {
				GenericSharePreference.getInstance().setOfflineMode(isChecked);
			} else if (ApLinkSwitch.this.mSwitchSpec.isToggleAutoSync()) {
				GenericSharePreference.getInstance().setEnableAutoSync(
				        isChecked);
			} else {
				ApLinkSwitch.this.toggle(ApLinkSwitch.this.group, isChecked);
			}
			ApLinkSwitch.this.handleTextMode(isChecked);
		}
	};
	private final Switch mSwitch;

	private final SwitchSpec mSwitchSpec;

	public ApLinkSwitch(final GenericFragment genericFragment,
	        final ControlSpec controlSpec, final View view, Bundle bundle) {
		super(genericFragment, controlSpec, view, bundle);
		this.mSwitch = (Switch) view;
		this.mSwitchSpec = (SwitchSpec) controlSpec;
		if (this.mSwitchSpec.getDisableGroupID() != null) {
			final int viewID = ResourcesHelper.getViewIdByName(this.mSwitchSpec
			        .getDisableGroupID());
			this.group = getActivity().findViewById(viewID);
		}
	}

	@Override
	public void clear() {

	}

	@Override
	public GenericAttribute getAttribute() {
		final String column = this.mSwitchSpec.getFullColumnName();
		final ObjPropInfo objPropInfo = getGenericFragment().getContentData()
		        .get(column);
		String value = null;
		if (this.mSwitch.isChecked()) {
			value = this.mSwitchSpec.getValueOn();
		} else {
			value = this.mSwitchSpec.getValueOff();
		}
		return GenericAttribute.obtain(value, objPropInfo);
	}

	@Override
	public GenericDictionary<String, GenericAttribute> getListAttributes() {
		return null;
	}

	@Override
	public Switch getView() {
		return this.mSwitch;
	}

	private void handleTextMode(final boolean isChecked) {
		if (isChecked) {
			if (!TextUtils.isEmpty(this.mSwitchSpec.getTextOn())) {
				this.mSwitch.setTextOn(Language.getInstance().get(
				        this.mSwitchSpec.getTextOn()));
			}
		} else {
			if (!TextUtils.isEmpty(this.mSwitchSpec.getTextOff())) {
				this.mSwitch.setTextOff(Language.getInstance().get(
				        this.mSwitchSpec.getTextOff()));
			}
		}
		this.mSwitch.setText(Language.getInstance().get(
		        this.mSwitch.getTag(R.id.TEXT_ENGLISH).toString()));
	}

	@Override
	protected void initViewProperties() {
		if (this.mSwitchSpec.isToggleOfflineMode()) {
			final boolean isOfflineMode = GenericSharePreference.getInstance()
			        .isOfflineMode();
			this.mSwitch.setChecked(isOfflineMode);
		} else if (this.mSwitchSpec.isToggleAutoSync()) {
			final boolean isAutoSync = GenericSharePreference.getInstance()
			        .isEnableAutoSync();
			this.mSwitch.setChecked(isAutoSync);
		}
		mSwitch.setTag(R.id.TEXT_ENGLISH, mSwitch.getText());
		handleTextMode(this.mSwitch.isChecked());
	}

	@Override
	public void languageChanged() {
		handleTextMode(this.mSwitch.isChecked());
	}

	@Override
	protected void loadBindingObject() {
		if (!this.mSwitchSpec.isToggleOfflineMode()) {
			final String value = getPassedObject().getValue(
			        this.mSwitchSpec.getFullColumnName());
			final boolean isChecked = value.equalsIgnoreCase(this.mSwitchSpec
			        .getValueOn());
			toggle(this.group, isChecked);
		}
	}

	@Override
	protected void loadDefaultObject() {
		if (!this.mSwitchSpec.isToggleOfflineMode()
		        && !this.mSwitchSpec.isToggleAutoSync()) {
			toggle(this.group, this.mSwitch.isChecked());
		}
	}

	@Override
	protected void setEventListener() {
		this.mSwitch.setOnCheckedChangeListener(this.mChangeListener);
	}

	@Override
	public void setObjPropInfoTemp(final OnGetObjProInfo getObjProInfo) {
		throw new UnsupportedOperationException(this.getClass().getSimpleName()
		        + " is not support this method");
	}

	@Override
	public void setOnClickListener(final OnClickListener onClickListener) {
		throw new UnsupportedOperationException(this.getClass().getSimpleName()
		        + " is not support this method");
	}

	private void toggle(final View view, final boolean isChecked) {
		if (view instanceof ViewGroup) {
			if (view instanceof Spinner) {
				view.setEnabled(isChecked);
			} else {
				for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
					toggle(((ViewGroup) view).getChildAt(i), isChecked);
				}
			}
		} else {
			view.setEnabled(isChecked);
		}
	}
}
